/**
* 交换两个变量的值
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

//typeof获得类型
#define SWAP(a, b){typeof(a) temp=a;a=b;b=temp;}

void SwapInt(int *, int *);

void Swap(void *, void *, size_t);

int main() {
    int a = 0;
    int b = 1;
    printf("a=%d,b=%d\n", a, b);
    //swap
    SwapInt(&a, &b);
    printf("a=%d,b=%d\n", a, b);
    Swap(&a, &b, sizeof(a));
    printf("a=%d,b=%d\n", a, b);
    SWAP(a, b)
    printf("a=%d,b=%d\n", a, b);
    return 0;
}

void SwapInt(int *left, int *right) {
    int temp = *left;
    *left = *right;
    *right = temp;
}

void Swap(void *left, void *right, size_t size) {
    void *temp = malloc(size);
    if (temp) {
        memcpy(temp, left, size);
        memcpy(left, right, size);
        memcpy(right, temp, size);
        free(temp);
    }
}